package com.lebang.controller.api.donwn;

import com.lebang.aop.Sessionid;
import com.lebang.common.Require;
import com.lebang.domain.EarnStatusRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.service.buss.MemberService;
import com.lebang.service.sys.AdBackLogService;
import com.lebang.service.sys.EarnStatusRecordService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @Author: hwq
 * @Date: 2020/7/14 15:49
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/down/cadencesapi")
public class CadencesAdi {
    @Autowired
    private MemberService memberService;

    @Autowired
    private EarnStatusRecordService earnStatusRecordService;
    @Autowired
    private AdBackLogService adBackLogService;

    @PostMapping("/add")
    @Sessionid
    @Require
    public JsonData add(HttpServletRequest request, String key, Integer type,String adId) {
        if(TextUtil.isEmpty(type)){
            return Callback.success("success", "查询成功", null);
        }
        RequestHeaders requestHeaders = new RequestHeaders(request);
        EarnStatusRecord record = new EarnStatusRecord();
        record.setCreateTime(LocalDateTime.now());
        record.setModifyTime(LocalDateTime.now());
        record.setHasDeleted(0);
        record.setIpAddress(requestHeaders.getIpAddr());
        record.setImei(requestHeaders.getImei());
        record.setAppStore(requestHeaders.getAppStore());
        record.setVersion(requestHeaders.getVersion());
        record.setMemberId(requestHeaders.getUserid());
        record.setSdkKey(key);
        record.setStatus(type);
        LocalDate date = memberService.selectCreateById(requestHeaders.getUserid()).toLocalDate();
        if (date.equals(LocalDate.now())) {
            record.setIsNewMember(1);
        } else {
            record.setIsNewMember(0);
        }
        earnStatusRecordService.insertModel(record);
        return Callback.success("success", "查询成功", null);
    }
    @PostMapping("/add1")
    @Sessionid
    @Require
    public JsonData add1(HttpServletRequest request, String key, Integer type,String adId) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        if(TextUtil.isEmpty(type)){
            return Callback.success("success", "查询成功", null);
        }
        earnStatusRecordService.add(requestHeaders,key,adId,type);
        return Callback.success("success", "查询成功", null);
    }

    @PostMapping("/record")
   // @Sessionid
    //@Require
    public JsonData record(HttpServletRequest request, String sdkKey, String json,String adId) {

        RequestHeaders requestHeaders = new RequestHeaders(request);
        if(TextUtil.isEmpty(json)){
            return Callback.success("success", "查询成功", null);
        }
        adBackLogService.add(requestHeaders,sdkKey,adId,json);
        return Callback.success("success", "查询成功", null);
    }


}
